package ui;

import dao.StudentApplicationDao;
import model.Student;
import model.StudentApplication;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class ApplicationRecordPanel extends JPanel {

    // 当前登录学生对象
    private final Student currentStudent;
    // 用于显示申报记录内容的面板
    private JPanel contentPanel;

    public ApplicationRecordPanel(Student student) {
        this.currentStudent = student;
        setLayout(new BorderLayout());

        // 设置粉色边框，视觉风格一致
        setBorder(BorderFactory.createLineBorder(new Color(255, 182, 193), 2));

        // 标题标签，居中显示
        JLabel title = new JLabel("申报记录与状态", JLabel.CENTER);
        title.setFont(new Font("微软雅黑", Font.BOLD, 24)); // 加粗大字体
        title.setForeground(new Color(255, 105, 180));    // 深粉色字体
        add(title, BorderLayout.NORTH); // 先添加到顶部，后面再调整布局

        // 顶部面板，方便放置刷新按钮和标题
        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(title, BorderLayout.CENTER);

        // 刷新按钮，点击后刷新申报记录列表
        JButton refreshButton = new JButton("刷新");
        refreshButton.setBackground(new Color(255, 192, 203));  // 浅粉色背景
        refreshButton.setFocusPainted(false);                   // 去掉聚焦虚线框
        refreshButton.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 统一字体风格
        topPanel.add(refreshButton, BorderLayout.EAST);

        // 把顶部面板放入主面板顶部，覆盖之前添加的title
        add(topPanel, BorderLayout.NORTH);

        // 主体内容面板，动态显示申报记录或者空白提示
        contentPanel = new JPanel(new BorderLayout());
        add(contentPanel, BorderLayout.CENTER);

        // 给刷新按钮绑定事件，点击刷新申报记录
        refreshButton.addActionListener(e -> refreshRecordList());

        // 界面初始化时加载申报记录
        refreshRecordList();
    }

    /**
     * 刷新申报记录列表，根据当前学生ID从数据库获取最新数据
     */
    private void refreshRecordList() {
        contentPanel.removeAll(); // 先清空内容

        // 通过DAO获取当前学生的所有申报记录列表
        List<StudentApplication> apps = new StudentApplicationDao().getApplicationList(currentStudent.getStudentId());

        if (apps.isEmpty()) {
            // 没有记录时显示提示
            JLabel emptyLabel = new JLabel("暂无申报记录", JLabel.CENTER);
            emptyLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            emptyLabel.setForeground(Color.GRAY);
            contentPanel.add(emptyLabel, BorderLayout.CENTER);
        } else {
            // 将申报记录转换为字符串数组用于JList显示
            String[] records = apps.stream()
                    .map(app -> "奖学金：" + app.getScholarshipName() + " | 状态：" + app.getStatus())
                    .toArray(String[]::new);

            // 创建JList显示申报记录
            JList<String> recordList = new JList<>(records);
            recordList.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            recordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 单选

            // 自定义渲染器，美化列表项样式
            recordList.setCellRenderer(new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                              boolean isSelected, boolean cellHasFocus) {
                    JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    // 每条记录底部加一条粉色分割线
                    label.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(255, 182, 193)));
                    label.setOpaque(true);
                    if (isSelected) {
                        label.setBackground(new Color(255, 228, 240)); // 选中时淡粉背景
                    } else {
                        label.setBackground(Color.WHITE);              // 未选中白底
                    }
                    return label;
                }
            });

            // 添加滚动面板容纳JList
            JScrollPane scrollPane = new JScrollPane(recordList);
            scrollPane.setPreferredSize(new Dimension(600, 400));

            // 撤回按钮，允许撤回待审核的申请
            JButton withdrawBtn = new JButton("撤回选中申请");
            withdrawBtn.setBackground(new Color(255, 192, 203));  // 浅粉色背景
            withdrawBtn.setFocusPainted(false);
            withdrawBtn.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            withdrawBtn.addActionListener(e -> {
                int idx = recordList.getSelectedIndex(); // 获取选中索引
                if (idx == -1) {
                    // 未选中任何条目，提示
                    JOptionPane.showMessageDialog(this, "请先选择一条待审核的申请", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                StudentApplication selectedApp = apps.get(idx);
                if (!"待审核".equals(selectedApp.getStatus())) {
                    // 只有状态为“待审核”的申请可以撤回
                    JOptionPane.showMessageDialog(this, "只有待审核状态的申请才能撤回", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 二次确认撤回操作
                int confirm = JOptionPane.showConfirmDialog(this, "确定撤回该申请？", "确认", JOptionPane.YES_NO_OPTION);
                if (confirm == JOptionPane.YES_OPTION) {
                    boolean success = new StudentApplicationDao().withdrawApplication(selectedApp.getId());
                    if (success) {
                        JOptionPane.showMessageDialog(this, "撤回成功");
                        refreshRecordList(); // 成功后刷新列表
                    } else {
                        JOptionPane.showMessageDialog(this, "撤回失败", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });

            // 面板布局，列表居中，撤回按钮居下
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(scrollPane, BorderLayout.CENTER);
            panel.add(withdrawBtn, BorderLayout.SOUTH);

            contentPanel.add(panel, BorderLayout.CENTER);
        }

        // 重新布局并刷新界面显示
        contentPanel.revalidate();
        contentPanel.repaint();
    }

}
